#include <signal.h>
#include "../comm/httplib.h"
#include "oj_model2.hpp"
#include "oj_control.hpp"

using namespace httplib;
using namespace ns_model;
using namespace ns_control;

static Control * ctl_ptr = nullptr;
void Recover(int signo)
{
    ctl_ptr->RecoverMachine();
}
int main()
{
    signal(SIGQUIT , Recover);
    EnableConsoleLogStrategy();
    // Model mod;
    // mod.ShowQuestions();

    // 0.启动服务器
    Server svr;
    Control ctl;
    ctl_ptr = &ctl;
    
    // 1.获取所有题目在线列表
    svr.Get("/all_questions", [&ctl](const Request& req, Response& resp){
        std::string html;
        ctl.AllQuestions(&html);
        resp.set_content(html , "text/html; charset=utf-8");
    });
    // 3.获取指定题目信息
    svr.Get(R"(/question/(\d+))", [&ctl](const Request& req, Response& resp){
        std::string number = req.matches[1];
        std::string html;
        ctl.OneQuestion(number, &html);
        resp.set_content(html , "text/html; charset=utf-8");
    });
    // 4.能够判题指定题目
     svr.Post(R"(/judge/(\d+))", [&ctl](const Request& req, Response& resp){
        std::string number = req.matches[1];
        std::string out_json; 
        ctl.Judge(number, req.body, &out_json); // req.body = 前端用户通过ajax传来的code--里面有code和input
        resp.set_content(out_json, "application=json; charset=utf-8");
    });

    svr.set_base_dir("./wwwroot");
    svr.listen("0.0.0.0", 8080);
    return 0;
}